# MCP-Link 统一管理 Makefile

.PHONY: help dev build deploy clean test status logs restart

# 默认目标
help:
	@echo "MCP-Link 项目管理命令："
	@echo ""
	@echo "开发环境:"
	@echo "  make dev          启动开发环境（热重载）"
	@echo "  make dev-frontend 只启动前端开发服务器"
	@echo "  make dev-backend  只启动后端开发服务器"
	@echo ""
	@echo "生产环境:"
	@echo "  make build        构建前端项目"
	@echo "  make deploy       完整部署到Docker"
	@echo "  make deploy-quick 快速重启服务（不重新构建）"
	@echo ""
	@echo "管理操作:"
	@echo "  make status       查看服务状态"
	@echo "  make logs         查看所有服务日志"
	@echo "  make logs-mcp     查看MCP服务日志"
	@echo "  make logs-frontend 查看前端服务日志"
	@echo "  make restart      重启所有服务"
	@echo "  make clean        清理构建文件和容器"
	@echo ""
	@echo "测试验证:"
	@echo "  make test         运行测试"
	@echo "  make verify       验证部署结果"

# 开发环境
dev:
	@echo "启动开发环境..."
	@if command -v cmd >/dev/null 2>&1; then \
		./dev-start.bat; \
	else \
		echo "Linux/Mac 开发环境启动..."; \
		echo "请手动启动："; \
		echo "1. cd frontend && npm run dev"; \
		echo "2. go run main.go serve --port 8080"; \
		echo "3. cd mcp_management_api && uvicorn main:app --reload"; \
	fi

dev-frontend:
	@echo "启动前端开发服务器..."
	cd frontend && npm run dev

dev-backend:
	@echo "启动后端开发服务器..."
	go run main.go serve --port 8080 --host 0.0.0.0

# 构建
build:
	@echo "构建前端项目..."
	cd frontend && npm install && npm run build
	@echo "✅ 前端构建完成"

# 部署
deploy:
	@echo "执行完整部署流程..."
	@if command -v cmd >/dev/null 2>&1; then \
		./deploy.bat; \
	else \
		bash ./deploy.sh; \
	fi

deploy-quick:
	@echo "快速重启服务..."
	docker compose restart

# 管理操作
status:
	@echo "查看服务状态..."
	docker compose ps

logs:
	@echo "查看所有服务日志..."
	docker compose logs -f

logs-mcp:
	@echo "查看MCP服务日志..."
	docker compose logs -f mcp-link

logs-frontend:
	@echo "查看前端服务日志..."
	docker compose logs -f frontend

logs-api:
	@echo "查看管理API日志..."
	docker compose logs -f management-api

restart:
	@echo "重启所有服务..."
	docker compose restart

# 清理
clean:
	@echo "清理构建文件..."
	@if exist frontend\\dist rmdir /s /q frontend\\dist
	@if exist frontend\\build rmdir /s /q frontend\\build
	@echo "停止并清理Docker容器..."
	docker compose down --volumes --remove-orphans
	@echo "清理Docker镜像..."
	docker system prune -f
	@echo "✅ 清理完成"

# 测试
test:
	@echo "运行前端测试..."
	cd frontend && npm test
	@echo "运行后端测试..."
	go test ./...

# 验证部署
verify:
	@echo "验证部署结果..."
	@echo "检查前端服务..."
	@curl -f http://localhost:3000 >/dev/null 2>&1 && echo "✅ 前端服务正常" || echo "❌ 前端服务异常"
	@echo "检查管理API..."
	@curl -f http://localhost:5001/api/v1/tools/ >/dev/null 2>&1 && echo "✅ 管理API正常" || echo "❌ 管理API异常"
	@echo "检查MCP服务..."
	@curl -f http://localhost:5050/logs >/dev/null 2>&1 && echo "✅ MCP服务正常" || echo "❌ MCP服务异常"

# 服务信息
info:
	@echo "🎯 MCP-Link 服务信息："
	@echo ""
	@echo "📋 服务访问地址："
	@echo "  • 前端管理界面: http://localhost:3000"
	@echo "  • 管理API:      http://localhost:5001"
	@echo "  • MCP服务:      http://localhost:5050"
	@echo "  • 日志API:      http://localhost:5050/logs"
	@echo ""
	@echo "📁 项目结构："
	@echo "  • frontend/     前端Vue.js项目"
	@echo "  • utils/        后端核心逻辑"
	@echo "  • mcp_management_api/ 管理API"
	@echo ""
	@echo "🔧 配置文件："
	@echo "  • docker-compose.yml  Docker编排"
	@echo "  • frontend/vite.config.ts Vite配置"
	@echo "  • go.mod               Go依赖"